﻿using HandyControl.Controls;
using Mapster;
using Minotaur.App.Core;
using Minotaur.App.Core.Attributes;
using Minotaur.App.Core.Mvvm;
using Minotaur.App.Core.Redis;
using Minotaur.App.Domains.Model.Table;
using Minotaur.App.Domains.Request;
using Minotaur.App.Domains.ViewModel;
using Minotaur.App.Domains.ViewModel.Logs;
using Minotaur.App.Domains.ViewModel.Table;
using Minotaur.App.Modules.Business.Service.SystemLog;
using Minotaur.App.Modules.SystemControl.Views;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace Minotaur.App.Modules.SystemSet.ViewModels
{
    public class SystemLogViewModel : WindowComputeViewModel
    {
        private ISystemLogService service;
        public SystemLogViewModel(IRegionManager regionManager, IDbContext dbContext, IRedisClient _redisClient, ISystemLogService _service) : base(regionManager, dbContext, _redisClient)
        {
            this.service = _service;
        }


        #region 属性注册区域 

        private ObservableCollection<SystemLogListViewModel> _Infos = new ObservableCollection<SystemLogListViewModel>();

        /// <summary>
        /// 数据表列表信息
        /// </summary>
        public ObservableCollection<SystemLogListViewModel> Infos { get => _Infos; set => SetProperty(ref _Infos, value); }
        #endregion

        /// <summary>
        /// 搜索方法
        /// </summary>
        public ICommand SearchCommand => new DelegateCommand(SearchAction);

        private void SearchAction()
        {
            InitTables();
        }
        private SystemControl.Views.Pagination pagination;
        public override void InitData()
        {
            pagination = this.ListPagination as SystemControl.Views.Pagination;
            pagination.ViewModel.PageEventClick += PageEventClick;
            InitTables();
        }

        private void InitTables()
        {
            OpendLoading = true;
            Task.Run(async () =>
            {
                SearcheRequest request = SearchViewModel.Adapt<SearcheRequest>();
                SystemLogListRespone tableInfo = await this.service.IniTableInfos(request);
                Dispatcher.Invoke(() =>
                {
                    try
                    {
                        if (tableInfo is null) return;
                        this.Infos = tableInfo.List.Adapt<ObservableCollection<SystemLogListViewModel>>();
                        foreach (var item in Infos)
                        {
                            item.DetaileAction = this.DetaileAction;
                        }
                        pagination.ViewModel.Total = tableInfo.TotalCount;
                        OpendLoading = false;
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                });
            });
        }

        [GlobalLogAspect]
        private void DetaileAction(object obj)
        {
        }
        private void PageEventClick(object sender, RoutedEventArgs e)
        {
            SearchViewModel searchViewModel = (SearchViewModel)e.OriginalSource;
            this.SearchViewModel.PageIndex = searchViewModel.PageIndex;
            this.SearchViewModel.PageSize = searchViewModel.PageSize;
            this.InitTables();
        } 
    }
}
